package com.lt.lumentracks.mapper;

import com.lt.lumentracks.entity.po.Conversation;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 会话表 Mapper 接口
 * </p>
 *
 * @author swl
 * @since 2025-10-06
 */
public interface ConversationMapper extends BaseMapper<Conversation> {

    // 查询当前用户所加入的会话
    @Select("SELECT " +
            "    c.*, " +
            "    IF(c.conversation_type = 1, cu_talker.user_id, NULL) AS talkerId " + // 根据会话类型判断，是否将【对方的用户id】赋值
            "FROM " +
            "    conversation c " +
            "JOIN " +
            "    conversation_user cu ON c.id = cu.conversation_id " +
            "LEFT JOIN " +
            "    conversation_user cu_talker ON c.id = cu_talker.conversation_id " +
            "                              AND cu_talker.user_id != #{userId} " +
            "                              AND c.conversation_type = 1   " + // 关键的过滤条件， 只在单聊时，才尝试查找对方ID
            "WHERE " +
            "    cu.user_id = #{userId} AND " +
            "    cu.clean_status != true " + // 不查询清空状态的会话
            "ORDER BY " +
            "    c.create_time DESC;")
    List<Conversation> queryRawByUser(Integer userId);
}
